﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Verse;

namespace Miao.Stand
{
    public static class MiaoStandUtility
    {

        public static IntVec3 GetStandGhostPos(this StandPawn standPawn)
        {
            int angle = standPawn.owner.Rotation.AsInt;//1234 rimworld是X_Z型2D
            switch (angle)
            {
                case 0://上
                    return new IntVec3(standPawn.owner.Position.x + 1, standPawn.owner.Position.y, standPawn.owner.Position.z - 1);
                    break;
                case 1://右
                    return new IntVec3(standPawn.owner.Position.x - 1, standPawn.owner.Position.y, standPawn.owner.Position.z + 1);
                    break;
                case 2://下
                    return new IntVec3(standPawn.owner.Position.x - 1, standPawn.owner.Position.y, standPawn.owner.Position.z + 1);
                    break;
                case 3://左
                    return new IntVec3(standPawn.owner.Position.x + 1, standPawn.owner.Position.y, standPawn.owner.Position.z - 1);
                    break;
            }
            Log.Error($"404，{standPawn}来到了不可能进入的坐标");
            return IntVec3.Zero;
        }
        private static int[] Sequence = new int[3] {1,0,-1 };
        public static void SetStandGhostPos(this StandPawn standPawn)
        {
            int angle = standPawn.owner.Rotation.AsInt;//1234 rimworld是X_Z型2D
            IntVec3 standPos = standPawn.Position;
            switch (angle)
            {
                case 0://上
                    for (int i = 0; i < Sequence.Length; i++)
                    {
                        standPos = new IntVec3(standPawn.owner.Position.x + Sequence[i], standPawn.owner.Position.y, standPawn.owner.Position.z - 1);
                        if (StandStandable(standPawn, standPos))
                        {
                            break;
                        }
                    }
                    break;
                case 1://右
                    for (int i = 0; i < Sequence.Length; i++)
                    {
                        standPos = new IntVec3(standPawn.owner.Position.x - 1, standPawn.owner.Position.y, standPawn.owner.Position.z - Sequence[i]);
                        if (StandStandable(standPawn, standPos))
                        {
                            break;
                        }
                    }
                    
                    break;
                case 2://下
                    for (int i = 0; i < Sequence.Length; i++)
                    {
                        standPos = new IntVec3(standPawn.owner.Position.x - Sequence[i], standPawn.owner.Position.y, standPawn.owner.Position.z + 1);
                        if (StandStandable(standPawn, standPos))
                        {
                            break;
                        }
                    }
                    break;
                case 3://左
                    for (int i = 0; i < Sequence.Length; i++)
                    {
                        standPos = new IntVec3(standPawn.owner.Position.x + 1, standPawn.owner.Position.y, standPawn.owner.Position.z + Sequence[i]);
                        if (StandStandable(standPawn, standPos))
                        {
                            break;
                        }
                    }
                    
                    break;
            }

        }
        static bool StandStandable(StandPawn standPawn, IntVec3 standPos)
        {
            if (standPos.Standable(standPawn.Map))
            {
                standPawn.Position = standPos;
                standPawn.Rotation = standPawn.owner.Rotation;
                standPawn.pather.StopDead();//路径可能缓存一些什么，要清除。
                return true;
            }
            return false;
        }

        public static void SetStandGhostPathPos(this StandPawn standPawn)
        {
            int angle = standPawn.owner.Rotation.AsInt;//1234 rimworld是X_Z型2D
            IntVec3 standToPos = standPawn.Position;
            switch (angle)
            {
                case 0://上
                    standToPos = new IntVec3(standPawn.owner.Position.x + 1, standPawn.owner.Position.y, standPawn.owner.Position.z - 1);
                    break;
                case 1://右
                    standToPos = new IntVec3(standPawn.owner.Position.x - 1, standPawn.owner.Position.y, standPawn.owner.Position.z + 1);
                    break;
                case 2://下
                    standToPos = new IntVec3(standPawn.owner.Position.x - 1, standPawn.owner.Position.y, standPawn.owner.Position.z + 1);
                    break;
                case 3://左
                    standToPos = new IntVec3(standPawn.owner.Position.x + 1, standPawn.owner.Position.y, standPawn.owner.Position.z - 1);
                    break;
            }
            if (standToPos.Standable(standPawn.Map))
            {
                standPawn.pather.StartPath(new LocalTargetInfo(standToPos), Verse.AI.PathEndMode.OnCell);
            }
        }




        public static void SetStandCover(this StandPawn standPawn)
        {
            int angle = standPawn.owner.Rotation.AsInt;//1234 rimworld是X_Z型2D
            IntVec3 standPos = standPawn.Position;
            switch (angle)
            {
                case 0://上
                    standPos = new IntVec3(standPawn.owner.Position.x, standPawn.owner.Position.y, standPawn.owner.Position.z + 1);

                    break;
                case 1://右
                    standPos = new IntVec3(standPawn.owner.Position.x + 1, standPawn.owner.Position.y, standPawn.owner.Position.z);
                    break;
                case 2://下
                    standPos = new IntVec3(standPawn.owner.Position.x, standPawn.owner.Position.y, standPawn.owner.Position.z - 1);
                    break;
                case 3://左
                    standPos = new IntVec3(standPawn.owner.Position.x - 1, standPawn.owner.Position.y, standPawn.owner.Position.z);
                    break;
            }
            if (standPos.Standable(standPawn.Map))//可站立
            {
                standPawn.Position = standPos;
                standPawn.Rotation = standPawn.owner.Rotation;
                standPawn.pather.StopDead();//路径可能缓存一些什么，要清除。
            }
        }
    }
}
